package com.example.joysinfo.lkc.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


/**
 * Created with IntelliJ IDEA.
 * User: bigkun
 * Email: bigkun@outlook.com
 * Date: 12-11-20
 * Time: 下午5:32
 */

public class AES {


    public static String encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        byte[] raw = sKey.getBytes("utf-8");
        if (raw.length != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return Base64.encode(encrypted, "utf-8");//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }

    public static String encrypt(String sSrc, byte[] sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey;
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return Base64.encode(encrypted, "utf-8");//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }
    public static String decrypt(String sSrc, byte[] sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
//                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length != 16) {
//                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey;
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            try {
//            	System.out.println("sSrc " + sSrc);
            	sSrc = sSrc.replace("_", "/");
            	sSrc = sSrc.replace("-", "\\+");
//            	System.out.println(sSrc);
            	byte[] encrypted1 = Base64er.decode(sSrc.getBytes());//先用base64解密
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
            System.out.println(ex.toString());
            return null;
        }
    }

    public static String decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.decode(sSrc.getBytes());//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
//        /*
//         * 此处使用AES-128-ECB加密模式，key需要为16位。
//         */
//        String cKey = "1234567890123456";
//        // 需要加密的字串
//        String cSrc = "www.gowhere.so";
//        System.out.println(cSrc);
//        // 加密
        String enString = AES.encrypt("11000000002915", "d8f44230260a4825");
//        String enString = AES.encrypt("11000000002915", Constants.key);
        System.out.println("加密后的字串是：" + enString); 
//
//        // 解密
      String DeString = AES.decrypt(enString, "d8f44230260a4825");
//      String DeString = AES.decrypt("havk+ZrhTAdPoH9Ea31vZw", Constants.key);
        System.out.println("解密后的字串是：" + DeString);
    }
    
    /**
     * diy的媒体id加密处理，解决diy图片、diy视频访问url漏洞
     * AES加密、base64加密、特殊字符处理
     * @return
     * @throws Exception
     */
    public static String diyMediaIdEncrypt(String mediaId) throws Exception {
//    	String encryptMediaId = AES.encrypt(mediaId, Constants.key);
    	String encryptMediaId = AES.encrypt(mediaId, "");
    	encryptMediaId = encryptMediaId.replace("/", "_");
    	encryptMediaId = encryptMediaId.replace("+", "-");
    	encryptMediaId = encryptMediaId.replace("=", "");
    	return encryptMediaId;
    }
}
